package cn.mgm.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class JDBCPrepared {
    public static void main(String[] args) throws Exception{

        // 获取用户输入信息
        System.out.println("请输入用户信息:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        // 连接数据库
        Connection connection = DriverManager.getConnection("jdbc:mysql://139.224.197.217:3336/guigu-auth?useSSL=false", "root", "kaa3akv8");


        // sql语句对象

        String sql = "SELECT * FROM sys_user where username=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        // 为 动态 SQL 赋值
        preparedStatement.setString(1,name);
        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()){
            System.out.println(resultSet.getString("name"));
        }

    }
}
